<template>
  <a-card :bordered="false">
    <!-- 左侧面板 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="6" :sm="8">
            <a-form-item
              label="字典名称"
              :maxLength="50">
              <a-input placeholder="请输入字典名称" v-model="queryParam.dictName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item
              label="字典编号">
              <a-input
                placeholder="请输入字典编号"
                v-model="queryParam.dictCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
                icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <div class="table-operator">
        <a-button
          @click="$refs.modalEdit.add()"
          type="primary"
          icon="plus"
          v-wx-role="50201">添加
        </a-button>
      </div>
      <s-table
        ref="table"
        rowKey="id"
        size="middle"
        :columns="columns"
        :data="loadData">
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-wx-role="50203">
            <a-icon type="edit"/>
            编辑
          </a>
          <a-divider type="vertical"/>
          <a @click="editDictItem(record.id)">
            <a-icon type="setting"/>
            字典配置</a>
          <a-divider type="vertical"/>
          <a-popconfirm
            title="确定删除吗?"
            @confirm="() =>handleDelete(record.id)"
            v-wx-role="50202">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>

      <dict-modal-edit ref="modalEdit" @ok="modalEditOk"></dict-modal-edit>
      <dict-item-list ref="itemList"></dict-item-list>
    </div>
  </a-card>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import { deleteAction, postAction } from '@/api/manage'
  import { baseMixin } from '@/store/app-mixin'
  import DictModalEdit from './DictModalEdit'
  import DictItemList from './DictItemList'

  export default {
    name: 'DictList',
    mixins: [baseMixin],
    components: { STable, Ellipsis, DictModalEdit, DictItemList },
    data () {
      return {
        visible: false,
        // 查询条件
        queryParam: {
          dictCode: '',
          dictName: ''
        },
        // 表头
        columns: [
          {
            title: '字典名称',
            dataIndex: 'dictName'
          },
          {
            title: '字典编号',
            dataIndex: 'dictCode'
          },
          {
            title: '描述',
            align: 'left',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          console.log('loadData.parameter', parameter)
          return postAction(this.url.list, Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },
        // 删除
        handleDelete (record) {
          deleteAction(this.url.delete, { ids: record }).then((resp) => {
            this.$message.success('删除成功!')
            this.$refs.table.refresh(true)
          })
        },
        dict: '',
        url: {
          list: '/system/sysDict/list',
          delete: '/system/sysDict/delete'
        }
      }
    },
    computed: {},
    methods: {
      // 用户编辑
      handleEdit (record) {
        this.$refs.modalEdit.edit(record)
      },
      searchQuery () {
        this.$refs.table.refresh(true)
      },
      // 保存刷新
      modalEditOk () {
        this.$refs.table.refresh()
      },
      // 查看
      editDictItem (record) {
        this.$refs.itemList.add(record)
      }
    },
    watch: {}
  }
</script>
<style scoped>
</style>
